/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PurchaseView.java
 *
 * Created on 27/05/2009, 7:06:38 PM
 */

package cp3120.view;

import cp3120.dao.DAOFactory;
import cp3120.model.Purchase;
import cp3120.model.Ticket;
import cp3120.net.ActiveUser;
import generic.swing.List2AbstractTableAdapater;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JTable;

/**
 *
 * @author adam
 */
public class PurchaseView extends javax.swing.JFrame {
    private List2AbstractTableAdapater<Ticket> ticketModel = new List2AbstractTableAdapater<Ticket>();
    private Purchase purchase;

    /** Creates new form PurchaseView */
    public PurchaseView(Purchase purchase) {
        this.purchase = purchase;

        ticketModel.setColumns(new String[] {"getFlight", "getFirstName", "getLastName", "getPrice", "getGST"},
                               new String[] {"Flight", "First", "Last", "Price", "GST"});
        ticketModel.setList(purchase.getTickets());

        initComponents();
        tickets.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        tickets.doLayout();
    }

    void addTicket(Ticket ticket) {
        ticketModel.add(ticket);
        purchase.applyPricingStrategy();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tickets = new javax.swing.JTable();
        abandon = new javax.swing.JButton();
        purchaseButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        tickets.setModel(ticketModel);
        jScrollPane1.setViewportView(tickets);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                      .addContainerGap()
                      .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 661, Short.MAX_VALUE)
                      .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                      .addContainerGap()
                      .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE)
                      .addContainerGap())
        );

        abandon.setText("Abandon");
        abandon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                abandonActionPerformed(evt);
            }
        });

        purchaseButton.setText("Purchase!");
        purchaseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                purchaseButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                      .addContainerGap()
                      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                          .addComponent(purchaseButton, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                          .addComponent(abandon, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                      .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                      .addContainerGap()
                      .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(abandon)
                                .addComponent(purchaseButton))
                      .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void purchaseButtonActionPerformed(java.awt.event.ActionEvent evt) { //GEN-FIRST:event_purchaseButtonActionPerformed
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        purchase.setUser(ActiveUser.getUser());
        purchase.setPurchaseDate(dateFormat.format(new Date()));
        purchase.applyPricingStrategy();

        DAOFactory.getPurchaseDAO().addPurchase(purchase);

        dispose();

    }//GEN-LAST:event_purchaseButtonActionPerformed

    private void abandonActionPerformed(java.awt.event.ActionEvent evt) { //GEN-FIRST:event_abandonActionPerformed
        purchase = new Purchase();
        dispose();
    }//GEN-LAST:event_abandonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton abandon;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton purchaseButton;
    private javax.swing.JTable tickets;
    // End of variables declaration//GEN-END:variables

}
